package com.example.no1admin.service.impl;

import com.example.no1admin.ex.ServiceException;
import com.example.no1admin.mapper.PackagesMapper;
import com.example.no1admin.mapper.PhotographerMapper;
import com.example.no1admin.mapper.PictureMapper;
import com.example.no1admin.pojo.dto.photographer.PhotographerAddNewDTO;
import com.example.no1admin.pojo.dto.photographer.PhotographerEditDTO;
import com.example.no1admin.pojo.entity.Photographer;
import com.example.no1admin.pojo.entity.Picture;
import com.example.no1admin.pojo.vo.packages.PackagesListItemVO;
import com.example.no1admin.pojo.vo.photographer.PhotographerDetailVO;
import com.example.no1admin.pojo.vo.photographer.PhotographerListItemVO;
import com.example.no1admin.pojo.vo.route.RouteListItemVO;
import com.example.no1admin.service.IPhotographerService;
import com.example.no1admin.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class PhotographerServiceImpl implements IPhotographerService {

    @Autowired
    private PhotographerMapper photographerMapper;
    @Autowired
    private PackagesMapper packagesMapper;
    @Autowired
    private PictureMapper pictureMapper;

    public PhotographerServiceImpl() {
        log.debug("创建业务逻辑对象：PhotographerServiceImpl");
    }

    @Override
    public void addNew(PhotographerAddNewDTO photographerAddNewDTO) {
        log.debug("开始处理添加摄影师的业务，参数：{}", photographerAddNewDTO);

        if (photographerAddNewDTO.getUrl() != null) {
            int count = pictureMapper.countByUrl(photographerAddNewDTO.getUrl());
            if (count != 1) {
                String message = "路线图片不存在！";
                log.error(message);
                throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
            }
            Picture picture = new Picture();
            picture.setAlbumId(photographerAddNewDTO.getAlbumId());
            picture.setUrl(photographerAddNewDTO.getUrl());
            picture.setIsCover(1);
            int rows = pictureMapper.updateByUrl(picture);
            if (rows != 1) {
                String message = "图片上传失败，服务器忙，请稍后再次尝试！";
                log.error(message);
                throw new ServiceException(ServiceCode.ERR_INSERT, message);
            }
        }
        // 检查此摄影师（尝试创建的摄影师）的名称有没有被使用
        // 如果已经被使用，则不允许创建
        String name = photographerAddNewDTO.getName();
        int count = photographerMapper.countByName(name);
        if (count > 0) {
            String message = "创建摄影师失败，摄影师名称【" + name + "】已经被占用！";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT, message);
        }

        // 创建实体对象（Mapper的方法的参数是实体类型）
        Photographer photographer = new Photographer();

        // 将当前方法参数的值复制到Brand实体类型的对象中
        BeanUtils.copyProperties(photographerAddNewDTO, photographer);
        // 补全属性值（不由客户端提交的属性的值，应该在插入之前补全）

        // 将摄影师数据写入到数据库中
        log.debug("即将向表中写入数据：{}", photographer);
        int rows = photographerMapper.insert(photographer);
        if (rows != 1) {
            String message = "创建摄影师失败，服务器忙，请稍后再次尝试！";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理删除摄影师的业务,id={}", id);
        PhotographerDetailVO photographer = photographerMapper.getById(id);
        if (photographer == null) {
            String message = "删除摄影师失败，尝试删除的数据【" + id + "】不存在！";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        List<PackagesListItemVO> packagesList = packagesMapper.list();
        for (PackagesListItemVO packages : packagesList) {
            if (packages.getRouteId().equals(id)) {
                String message = "删除摄影师失败，" + packages.getName() + "套餐内包含该摄影师,请核验！";
                throw new ServiceException(ServiceCode.ERR_DELETE, message);
            }
        }

        int rows = photographerMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除摄影师失败，服务器忙，请稍后再试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
    }

    @Override
    public void deleteByIds(Long... ids) {
        log.debug("开始处理删除摄影师的业务,ids={}", ids);
        int rows = photographerMapper.deleteByIds(ids);
    }

    @Override
    public void updateById(PhotographerEditDTO photographerEditDTO) {
        PhotographerDetailVO photographerDetailVO = photographerMapper.getById(photographerEditDTO.getId());
        if (photographerDetailVO == null) {
            String message = "修改摄影师失败，尝试修改的数据【" + photographerEditDTO.getId() + "】不存在！";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        Photographer photographer = new Photographer();
        BeanUtils.copyProperties(photographerEditDTO, photographer);
        //将摄影师数据写入到数据库
        log.debug("即将向表中插入需要修改的数据：{}", photographer);
        int rows = photographerMapper.updateById(photographer);
        if (rows != 1) {
            String message = "修改摄影师失败，服务器忙，请稍后再试!";
            log.error(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

    @Override
    public List<PhotographerListItemVO> list() {
        
        return photographerMapper.list();
    }
}
